/**
 * imported packages
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @authors BENSON WACHIRA, DOROTHY SARPONG, STANLEY ANKU
 * MICHAEL ESHUN & MAKAFUI AMEZAH
 */

/**
 * 
 * Controller for the delete flight form
 */
public class ControllerDeleteFlight {
    
    /**
     * variables and object declarations
     */
    Connection conn=null;
    String deleteF;
    Statement st=null;
    public DeleteFlight deleteFlt;
    private ActionListener actionListener;
    
    /**
     * constructor for instantiating objects
     */
    public ControllerDeleteFlight( ){
       // this.deleteOrder = delete;
        deleteFlt = new DeleteFlight();
        deleteFlt.setVisible(true);
                          
    }
  
   /**
    * Method to delete  flight
    */
    public void deleteFlight(){
        try{
        Class.forName("com.mysql.jdbc.Driver");
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
        deleteF="DELETE FROM Flights where fName ='"+deleteFlt.getKey()+"';" ;
        st=conn.createStatement();
        st.execute("USE jetwingdb;");
        st.executeUpdate(deleteF);
        
        st.close();
        conn.close();
        JOptionPane.showMessageDialog(null,"Flight has been deleted");
        }
        catch(Exception e){
         JOptionPane.showMessageDialog(null,e);
        }
    }
    
    /**
     * Method to get sources of button clicked using the action listener
     */
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==deleteFlt.getDeleteButton()){
                        deleteFlight();
                  }
                  if(actionEvent.getSource()==deleteFlt.getCloseButton()){
                        deleteFlt.setVisible(false);
                  }
                  }
              
        };                
        deleteFlt.getDeleteButton().addActionListener(actionListener);
        deleteFlt.getCloseButton().addActionListener(actionListener);
        } 
}
